home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1994 #2
/
Monster Media No. 2 (Monster Media)(1994).ISO
/
soundu
/
dg2.zip
/
MARKOV
/
MARKOV.BAS
< prev
next >
Wrap
BASIC Source File
|
1993-11-17
|
5KB
|
168 lines
DECLARE SUB addword (word$, atword%, chainindex%, setword%)
DECLARE FUNCTION getchain$ (number%)
DECLARE SUB makewords (link$, w1$, w2$, w3$)
DECLARE FUNCTION getword$ (fileno%)
DEFINT A-Z
x$ = COMMAND$
IF COMMAND$ = "" THEN
PRINT "MARKOV filein.ext fileout.ext"
END
END IF
x$ = x$ + " a b c"
makewords x$, filein$, filefinal$, j$
fileno1 = FREEFILE
OPEN filein$ FOR BINARY AS #fileno1
filein$ = "test.chn"
fileno2 = FREEFILE
OPEN filein$ FOR OUTPUT AS #fileno2
CLS
PRINT "SHANNON'S MARKOV CHAIN WRITING PROGRAM"
PRINT
PRINT "Working... Number of chains created:";
fw$ = getword$(fileno1)
sw$ = getword$(fileno1)
at = 0
DO UNTIL EOF(fileno1)
at = at + 1
LOCATE 3, 38: PRINT at;
tw$ = getword$(fileno1)
chain$ = fw$ + " " + sw$ + " " + tw$
PRINT #fileno2, chain$
fw$ = sw$: sw$ = tw$
IF at = 5000 THEN
PRINT "...maxed out"
EXIT DO
END IF
LOOP
CLOSE
DIM SHARED chaini(1 TO at, 1 TO 3) 'integer reference #'s
DIM SHARED chainw$(1 TO at \ 2) 'words being referenced: assume half the words are repeats... hopefully
DIM SHARED chainf$(1 TO 150) 'for probability branch
DIM SHARED chainfi(1 TO 150) 'and index
RANDOMIZE TIMER
LOCATE 4, 1
PRINT " [SLOW] Number of chains loaded:";
OPEN "test.chn" FOR INPUT AS #1
LINE INPUT #1, temp$
makewords temp$, w1$, w2$, w3$
chaini(1, 1) = 1: chaini(1, 2) = 2: chaini(1, 3) = 3
chainw$(1) = w1$: chainw$(2) = w2$: chainw$(3) = w3$
prev2 = 2: prev3 = 3
atw = 4 'at word #
atc = 2 'at chain #
topw = 3
FOR r = 2 TO at
LOCATE 4, 38: PRINT r;
LINE INPUT #1, temp$
makewords temp$, w1$, w2$, w3$
curr3 = 0
addword w1$, atw, chaini(r, 1), prev2
addword w2$, atw, chaini(r, 2), prev3
addword w3$, atw, chaini(r, 3), curr3
prev2 = prev3: prev3 = curr3
IF curr3 > topw THEN topw = curr3
NEXT
CLOSE #1
PRINT "..." + LTRIM$(STR$(topw)) + " distinct words"
OPEN filefinal$ FOR OUTPUT AS #2
link$ = getchain$(1)
PRINT ""
PRINT ""
oldr = 1
DO
makewords link$, w1$, w2$, w3$
PRINT w1$; " ";
PRINT #2, w1$; " ";
chain2$ = w2$ + " " + w3$
nf = 0
FOR r = 1 TO at
x = LEN(chain2$)
IF chaini(oldr, 2) = chaini(r, 1) AND chaini(oldr, 3) = chaini(r, 2) THEN
nf = nf + 1
chainf$(nf) = getchain$(r)
chainfi(nf) = r
END IF
NEXT
IF nf = 0 THEN
oldr = INT(RND * (at)) + 1
link$ = getchain$(oldr)
PRINT "": PRINT ""
PRINT #2, "": PRINT #2, ""
ELSE
sel = INT(RND * nf) + 1
oldr = chainfi(sel)
link$ = chainf$(sel)
END IF
LOOP UNTIL INKEY$ <> ""
CLOSE
SUB addword (word$, atword, chainindex, setword)
IF setword <> 0 THEN
chainindex = setword
ELSE
found = 0
FOR r = 1 TO atword
IF chainw$(r) = word$ THEN
found = r
EXIT FOR
END IF
NEXT
IF found = 0 THEN
atword = atword + 1
found = atword
chainw$(found) = word$
END IF
chainindex = found
setword = found
END IF
END SUB
FUNCTION getchain$ (number)
getchain$ = chainw$(chaini(number, 1)) + " " + chainw$(chaini(number, 2)) + " " + chainw$(chaini(number, 3))
END FUNCTION
FUNCTION getword$ (fileno)
x$ = ""
DO
x$ = INPUT$(1, #fileno)
IF LEN(x$) = 0 THEN EXIT DO
IF ASC(x$) > 32 THEN EXIT DO
LOOP UNTIL EOF(fileno)
DO
y$ = INPUT$(1, #fileno)
IF LEN(y$) = 0 THEN EXIT DO
IF ASC(y$) > 32 THEN
x$ = x$ + y$
ELSE
EXIT DO
END IF
LOOP UNTIL EOF(fileno)
getword$ = x$
END FUNCTION
SUB makewords (link$, w1$, w2$, w3$)
x1 = INSTR(link$, " ")
x2 = INSTR(x1 + 1, link$, " ")
w1$ = MID$(link$, 1, x1 - 1)
w2$ = MID$(link$, x1 + 1, x2 - x1 - 1)
w3$ = MID$(link$, x2 + 1, LEN(link$) - x2)
END SUB